package com.jy.adscm.operation.purchase.controller;



import com.jy.adscm.operation.purchase.bean.BasePurchase;
import com.jy.adscm.operation.purchase.service.BasePurchaseService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *采购申请基础表
 * @author lzf
 * @since 2019-04-22
 */
@RestController
@RequestMapping("adscm/operation/basePurchase")
public class BasePurchaseController {
    @Autowired
    private BasePurchaseService basePurchaseService;
    @Autowired
    private RedisTemplate<Object, Token> tokenRedisTemplate;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody BasePurchase basePurchase, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        basePurchase.setApplyPerson(token.getUser().getId());
        basePurchase.setPlanTotal(basePurchase.getPlanPrice()*basePurchase.getPlanNums());
        return new SimpleCoreResponse("200","新增成功",basePurchaseService.save(basePurchase));
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody BasePurchase basePurchase){
        return new SimpleCoreResponse("200","修改成功",basePurchaseService.updateById(basePurchase));
    }
    @RequestMapping("delete")
    public SimpleCoreResponse delete(Long id){
        return new SimpleCoreResponse("200","删除成功",basePurchaseService.deleteById(id));
    }
    @RequestMapping("queryByStatus")
    public SimpleCoreResponse queryByStatus(HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        int applyPerson=token.getUser().getId();
        return new SimpleCoreResponse("200","查询成功",basePurchaseService.queryByStatus(applyPerson));
    }
}

